<?php

class M_user extends CI_Model {
	
	private $table_name = "zy_user";

    public function __construct()
    {
        parent::__construct();
		$this->load->database();
    }
    
	/**
     * 根据用户ID检索用户信息
     *
     * @param string $email 用户邮箱地址
     *
     * @return array 查询到的用户个人信息组成的数组
     */
    public function selectUserById($user_id)
    {
    	return $this->db->get_where( $this->table_name ,array( 'user_id' => $user_id ))->result();
    }
    
    /**
     * 根据用户名检索用户信息
     * 
     * @param string $nickname 用户的昵称
     * 
     * @return array 查询到的用户个人信息组成的数组
     */
    public function selectUserByNickname($nickname)
    {
    	return $this->db->get_where( $this->table_name ,array( 'user_nickname' => $nickname ))->result();
    }
    
    /**
     * 根据用户邮箱地址检索用户信息
     *
     * @param string $email 用户邮箱地址
     *
     * @return array 查询到的用户个人信息组成的数组
     */
    public function selectUserByEmail($email)
    {
    	return $this->db->get_where( $this->table_name ,array( 'user_email' => $email ))->result();
    }
    
    /**
     * 根据用户ID检索用户信息
     *
     * @param string $user_id 用户ID
     *
     * @return array 查询到的用户个人信息组成的数组
     */
    /*public function selectUserMoreByID($user_id)
    {
    	$query = "SELECT u.*,ud.*
    			 FROM `fz_user` as u,`fz_user_data` as ud
    			WHERE u.`user_id`=ud.`user_id`
    			AND u.`user_id`='".$user_id."'";
    	return $this->db->query($query)->result();
    }*/
    
    /**
     * 更换用户头像
     */
    public function updateUserHeadimg($head_pic)
    {
    	$data = array('head_pic'=>$head_pic);
    	$this->db->where("user_id",$this->session->userdata('user_id'));
    	if($this->db->update($this->table_name,$data))
    	{
    		return true;
    	}
    }
    
    /**
     * 用户注册
     * 
     * @param string $nickname 用户的昵称
     * @param string $password 用户的密码,已经加密
     * @param string $email 用户的邮箱地址
     * @param string $validcode 用户的密码加密秘钥
     * 
     * @return int insert_id 用户的ID
     */
    public function insertUser($nickname,$password,$email,$validcode)
    {
    	$data = array('user_nickname'=>$nickname,
    			'user_password'=>$password,
    			'user_email'=>$email,
    			'validcode'=>$validcode,
    	);
    	if($this->db->insert( $this->table_name ,$data))
    	{
    		return $this->db->insert_id();
    	}
    }
    
    /**
     * 创建用户详细信息表记录
     * 
     * @param int $user_id 用户的ID
     * 
     * @return void
     */
    public function insertUserData($user_id)
    {
    	$data = array('user_id'=>$user_id ,'register_time'=>time(),'login_time'=>time());
    	$this->db->insert('zy_user_data',$data);
    }
    
    /**
     * 检索出用户列表
     *
     * @param int $page 当前页码
	 * @param int $pagesize 每页记录的数量
     *
     * @return array 用户信息组成的数组
     */
    /*public function selectUser($page,$pagesize,$condition)
    {
    	$where = '';
    	if($condition['user_id'])
    	{
    		$where .= "AND u.`user_id`='".$condition['user_id']."'";
    	}
		
		if($condition['user_name'])
    	{
    		$where .= "AND u.`user_nickname` = '".$condition['user_name']."'";
    	}
    	 
    	if($condition['user_truename'])
    	{
    		$where .= "AND ud.`real_name` LIKE '%".$condition['user_truename']."%'";
    	}
    	 
    	if($condition['email'])
    	{
    		$where .= "AND u.`user_email` ='".$condition['email']."'";
    	}
    	
    	if($condition['phone'])
    	{
    		$where .= "AND ud.`mobile` ='".$condition['phone']."'";
    	}
		
    	if($condition['qq'])
    	{
    		$where .= "AND ud.`qq` ='".$condition['qq']."'";
    	}
    	
    	if($condition['sex'])
    	{
    		$where .= "AND ud.`sex` ='".$condition['sex']."'";
    	}
    	$query = "SELECT u.*,ud.*
    			 FROM `fz_user` as u,`fz_user_data` as ud
    			WHERE u.`user_id`=ud.`user_id` ".$where." 
    			ORDER BY u.`user_id` DESC";
    	return $this->db->query($query)->result();
    }*/
	
	/**
	 * 查询用户数量
	 * 
	 * 
	 */
    /*public function selectUserAmount($condition)
    {
    	$where = '';
    	if($condition['user_id'])
    	{
    		$where .= "AND u.`user_id`='".$condition['user_id']."'";
    	}
		
		if($condition['user_name'])
    	{
    		$where .= "AND u.`user_nickname` = '".$condition['user_name']."'";
    	}
    	 
    	if($condition['user_truename'])
    	{
    		$where .= "AND ud.`real_name` LIKE '%".$condition['user_truename']."%'";
    	}
    	 
    	if($condition['email'])
    	{
    		$where .= "AND u.`user_email` ='".$condition['email']."'";
    	}
    	
    	if($condition['phone'])
    	{
    		$where .= "AND ud.`mobile` ='".$condition['phone']."'";
    	}
    	
    	if($condition['qq'])
    	{
    		$where .= "AND ud.`qq` ='".$condition['qq']."'";
    	}
    	
    	if($condition['sex'])
    	{
    		$where .= "AND ud.`sex` ='".$condition['sex']."'";
    	}
    	$query = "SELECT COUNT(*) AS userAmount FROM `fz_user` as u,`fz_user_data` as ud WHERE u.`user_id`=ud.`user_id` ".$where;
    	return $this->db->query($query)->result();
    }*/
    
    /**
     * 修改用户状态
     *
     * @param int $user_id 用户ID
	 * @param int $status 用户的状态
     *
     * @return boolean
     */
    /*public function updateUserStatus($user_id,$status)
    {
    	$data=array('user_status'=>$status);
    	$this->db->where('user_id',$user_id);
    	if($this->db->update('fz_user',$data))
    	{
    		return true;
    	}
    }*/
    
    /**
     * 更新用户基本信息表
     *
     * @param int $user_id 用户ID
	 * @param array $user_info 用户信息组成的数组
     *
     * @return boolean
     */
    public function updateUserBaseInfo($user_id,$nickname)
    {
    	$data = array('user_nickname'=>$nickname);
    	$this->db->where('user_id',$user_id);
    	if($this->db->update( $this->table_name ,$data))
    	{
    		return true;
    	}

 
    }
    
	/**
	 * 更新用户详细信息表
	 */
    /*public function updateUserMoreInfo($user_id,$data)
    {
    	$this->db->where('user_id',$user_id);

    	if($this->db->update('fz_user_data',$data))
    	{
    		return true;
    	}
    }*/
    
    /**
     * 给用户账户充值
	 * 
	 * @param float $balance 用户需要充值的钱
	 * @param int   $user_id 用户ID
	 * 
	 * @return boolean
     */
    /*public function userReCharge($balance,$user_id,$reduce)
    {
    	if($reduce)
		{
			$query = "UPDATE `fz_user_data` SET balance=balance-".$balance." WHERE `user_id`='".$user_id."'";
		}
		else
		{
			$query = "UPDATE `fz_user_data` SET balance=balance+".$balance." WHERE `user_id`='".$user_id."'";
		}
    	
    	if($this->db->query($query))
    	{
    		return true;
    	}
    }*/
	
	/**
	 * 获取用户的余额
	 */
	/*public function selectUserBalance($user_id)
	{
		$this->db->select('balance');
		return $this->db->get_where('fz_user_data',array('user_id'=>$user_id))->result();
	}*/
	
	/**
	 * 增加用户地址
	 * 
	 * @param string $user_name 用户的真实姓名
	 * @param string $user_phone 用户的手机号码
	 * @param string $user_address 用户的收货地址
	 * 
	 * @return boolean
	 */
	/*public function addUserAddress($user_name,$user_phone,$user_address)
	{
		//现将用户所有的地址都设置为不默认
		if($this->setUserAddressDefaultZone($this->session->userdata('user_id')))
		{
			$data = array('user_id'=>$this->session->userdata('user_id'),
				"user_name"=>$user_name,
				"phone"=>$user_phone,
				"province"=>"",
				"city"=>"",
				"area"=>"",
				"address"=>$user_address
			);
			//增加的，数据库会默认设置为1，默认值
			if($this->db->insert("fz_address",$data))
			{
				return true;
			}
		}
		
	}*/
	
	/**
	 * 将一个用户的所有地址都设置为不默认状态（is_default=0）
	 * 
	 * @param int $user_id 用户的ID
	 * 
	 * @return boolean
	 */
    /*public function setUserAddressDefaultZone($user_id)
	{
		$data = array("is_default"=>"0");
		$this->db->where("user_id",$user_id);
		if($this->db->update("fz_address",$data))
		{
			return true;
		}
	}*/
	
	/**
	 * 检索出收货地址
	 * 
	 * @return object 用户收货地址组成的数组变量
	 */
	/*public function selectUserAddress()
	{
		return $this->db->get_where("fz_address",array("user_id"=>$this->session->userdata("user_id")))->result();
	}*/
	
	/**
	 * 更新用户信息
	 * 
	 * @param int $user_id 用户ID
	 * @param array $data 用户需要更新的信息组成的数组
	 * 
	 * @return boolean
	 */
	/*public function updateUserInfo($user_id,$data)
	{
		$query = "UPDATE `fz_user`,`fz_user_data` 
		SET fz_user.`user_nickname`='".$data['nickname']."',
		fz_user_data.`real_name`='".$data['real_name']."',
		fz_user_data.`mobile`='".$data['mobile']."',
		fz_user_data.`qq`='".$data['qq']."',
		fz_user_data.`sex`='".$data['sex']."',
		fz_user_data.`birthday`='".$data['birth_year']."-".$data['birth_month']."'
		WHERE fz_user.`user_id`='".$user_id."' AND fz_user_data.`user_id`='".$user_id."'";
		if($this->db->query($query))
		{
			return true;
		}
	}*/
}